<?php
namespace App\Http\Controllers\Admin\Product;

use App\Http\Controllers\Admin\BaseController;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;

class BrandController extends BaseController
{
    protected  $api_url = [
        'index' => ['get', 'product/brand'],
        'show' => ['get', 'product/brand/%d'],
        'store' => ['post', 'product/brand'],
        'update' => ['put', 'product/brand/%d'],
        'delete' => ['delete', 'product/brand/%d'],
        
        'cates'=>['get', 'product/cate'],
    ];
    
    protected $api_host;
    protected $request;
    
    public function __construct(Request $request)
    {
        $this->request = $request;
        $this->api_host = config('url.api_backend');
    }
    
    public function indexAction(){
        $this->validate($this->request, [
            'page' => 'integer',
            'name' => 'string',
        ]);
        $search_params = $this->request->all(['name']);
        $page = $this->request->get('page', 1);
        $params = [
            'search' => search_params($search_params),
            'searchJoin' => 'and',
            'orderBy'=>'id' ,
            'sortedBy'=>'desc',
            'page' => $page
        ];
        
        $ret = http_admin($this->api_url['index'])->send($params); 
        $data = [];
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            $data = $ret['data'];
        }
        
        $data['search_params'] = $search_params;
        
        $params = [
            'type' => 'all',
        ];
        
     
        //这里只读类目 不对数据做存redis处理
        $data['cates'] = [];
        if(Redis::exists("categorys")){
            $cateall = Redis::hgetall("categorys");
            if(is_array($cateall)){
                foreach($cateall as $c){
                    $data['cates'] [] = json_decode($c,true);
                }
                $data['cates'] = sortArrByManyField( $data['cates'],'root_id',SORT_ASC,'pid',SORT_ASC,'sort',SORT_ASC,'id',SORT_ASC);
            }
        }else{
            $cates_ret = http_admin($this->api_url['cates'])->send($params);
            if(isset($cates_ret['code']) && $cates_ret['code']===0 && !empty($cates_ret['data'])){
                $data['cates'] = $cates_ret['data']['data'];
 
            }
        }
        //
        
        
        return view('Admin.product.brand', paginate($this->request, $data));
    }
    
    public function showAction($id){
        echo $id;
    }
    
    public function storeAction(){
        $this->validate($this->request, [
            'name' => 'string',
        ]);
        $data = $this->request->all([]);
        
        $ret = http_admin($this->api_url['store'])->send($data); 
         
        if(isset($ret['code']) && $ret['code']===0 && !empty($ret['data'])){
            Redis::hset("brand",$ret['data']['id'],json_encode($ret['data']));
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>isset($ret['message']) ? $ret['message'] : $ret['data']]);
        }
    }
    
    public function updateAction($id){
        $this->validate($this->request, [
            'name' => 'required|string',
        ]);
        $data = $this->request->all([]);
        
        $ret = http_admin($this->api_url['update'],[$id])->send($data); 
        if(isset($ret['code']) && $ret['code']===0 ){
            $brand = http_admin($this->api_url['show'],[$id])->send();
            if(isset($brand['data']['data'])){
                Redis::hset("brand",$id,json_encode($brand['data']['data']));
            }
            return response()->json(['code'=>0,'message'=>'']);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
    
    public function deleteAction($id){
        $ret = http_admin($this->api_url['delete'],[$id])->send(); 
        if(isset($ret['code']) && $ret['code']===0){
            Redis::hdel("brand",$id);
            return response()->json(['code'=>0,'message'=>$ret['message']]);
        }else{
            return response()->json(['code'=>1,'message'=>$ret['message']]);
        }
    }
}